package edu.sfsu.powerrangers.jeopardy.server;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import edu.sfsu.powerrangers.jeopardy.ContestantClient;
import edu.sfsu.powerrangers.jeopardy.gamestate.Contestant;

public class ContestantCollection {
	private Vector<String> names;
	private Vector<Contestant> dms;
	private Vector<ContestantClient> clients;
	private Map<String, Contestant> nameToDM;
	private Map<String, ContestantClient> nameToClient;
	private Map<Contestant, ContestantClient> dmToClient;
	private Map<ContestantClient, Contestant> clientToDM;
	
	public ContestantCollection() {
		clear();
	}
	
	public void clear() {
		names = new Vector<String>();
		dms = new Vector<Contestant>();
		clients = new Vector<ContestantClient>();
		nameToDM = new HashMap<String, Contestant>();
		nameToClient = new HashMap<String, ContestantClient>();
		dmToClient = new HashMap<Contestant, ContestantClient>();
		clientToDM = new HashMap<ContestantClient, Contestant>();
	}
	
	public void add(ContestantClient cc, Contestant c) {
		names.add(c.getName());
		dms.add(c);
		clients.add(cc);
		nameToDM.put(c.getName(), c);
		nameToClient.put(c.getName(), cc);
		dmToClient.put(c, cc);
		clientToDM.put(cc, c);
	}
	
	public void remove(String name) {
		Contestant c = nameToDM.get(name);
		ContestantClient cc = nameToClient.get(name);
		int index = clients.indexOf(cc);
		names.remove(index);
		dms.remove(index);
		clients.remove(index);
		nameToDM.remove(name);
		nameToClient.remove(name);
		dmToClient.remove(c);
		clientToDM.remove(cc);
	}
	
	public void remove(Contestant c) {
		String name = c.getName();
		ContestantClient cc = dmToClient.get(c);
		int index = clients.indexOf(cc);
		names.remove(index);
		dms.remove(index);
		clients.remove(index);
		nameToDM.remove(name);
		nameToClient.remove(name);
		dmToClient.remove(c);
		clientToDM.remove(cc);
	}
	
	public void remove(ContestantClient cc) {
		Contestant c = clientToDM.get(cc);
		String name = c.getName();
		int index = clients.indexOf(cc);
		names.remove(index);
		dms.remove(index);
		clients.remove(index);
		nameToDM.remove(name);
		nameToClient.remove(name);
		dmToClient.remove(c);
		clientToDM.remove(cc);
	}
	
	public String getName(String name) { return name; }
	public String getName(Contestant c) { return c.getName(); }
	public String getName(ContestantClient cc) { return clientToDM.get(cc).getName(); }
	public String[] getNames() { return names.toArray(new String[0]); }
	
	public Contestant getDM(String name) { return nameToDM.get(name); }
	public Contestant getDM(Contestant c) { return c; }
	public Contestant getDM(ContestantClient cc) { return clientToDM.get(cc); }
	public Contestant[] getDMs() { return dms.toArray(new Contestant[0]); }
	
	public ContestantClient getClient(String name) { return nameToClient.get(name); }
	public ContestantClient getClient(Contestant c) { return dmToClient.get(c); }
	public ContestantClient getClient(ContestantClient cc) { return cc; }
	public ContestantClient[] getClients() { return clients.toArray(new ContestantClient[0]); }
}
